package cn.net.nit.track.dao.impl;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import cn.net.nit.track.dao.GPSDao;
import cn.net.nit.track.entry.GPSEvent;

public class GPSDaoImpl implements GPSDao {
	
	public static SessionFactory sessionFactory;
	
	static{
		Configuration cfg = new Configuration();
		sessionFactory = cfg.configure().buildSessionFactory();
	}

	public void insertNewGpsLocation(GPSEvent event) {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.save(event);
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
	} 
	
	public GPSEvent getLocationByMobile(long mobileNum){
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("from GPSEvent as e where e.mobileNum =:mobileNum");
		query.setParameter("mobileNum", mobileNum);
		return (GPSEvent) query.list().get(0);
	}

}
